package com.example.arcapplication.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by WYJ on 2020/7/30.
 */
public class CommonViewHolder<T> extends RecyclerView.ViewHolder {

    private ViewDataBinding binding;

    public CommonViewHolder(View v) {
        super(v);
        binding = DataBindingUtil.bind(v);
    }

    public static <T> CommonViewHolder<T> get(Context context, ViewGroup viewGroup, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, viewGroup, false);
        return new CommonViewHolder<>(itemView);
    }

    public void setBinding(int variableId, Object object) {
        binding.setVariable(variableId, object);
        binding.executePendingBindings();
    }

    public ViewDataBinding getBinding() {
        return binding;
    }

    public void bind(int variableId, T t) {
        binding.setVariable(variableId, t);
    }
}